/**
 * 
 */
package cx3k.GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.border.*;
import javax.swing.*;

import cx3k.scheduler.objects.Course;

/**
 * @author Chris
 *
 */
public class SwapClassDisplay extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public JButton jbOptions, jbRemove;
	private ClassMouseListener CML;
	private JPopupMenu jpm;
	private ClassInfo classInfo;
	private ScheduledCourseGroup courseGroup;
	public JMenuItem jmiRemove;
	private MenuListener ML;
	private Course courses[];
	private JLabel jlCourse;
	private JComboBox jcbCourses;
	public final boolean PLACEHOLDER;
	private ChangeListener CL;
	
	public SwapClassDisplay(ScheduledCourseGroup courseGroup, ClassInfo classInfo) {
		this.setPreferredSize(new Dimension(150, 30));
		this.setBackground(MainWindow.MAINCOLOR);
		this.setBorder(new LineBorder(Color.black));
		this.classInfo = classInfo;
		this.setCourseGroup(courseGroup);
		this.courses = courseGroup.getCourses();
		this.PLACEHOLDER = false;
		
		this.CML = new ClassMouseListener();
		
		this.jbRemove = new JButton("<X>");
		this.jbRemove.setBorder(null);
		this.jbRemove.setCursor(new Cursor(Cursor.HAND_CURSOR));
		this.add(jbRemove);
		
		this.jbOptions = new JButton("     >>>");
		this.jbOptions.setBackground(MainWindow.MAINCOLOR);
		this.jbOptions.setBorder(null);
		this.jbOptions.setCursor(new Cursor(Cursor.HAND_CURSOR));
		this.jbOptions.addActionListener(new OptionListener());
		
		this.ML = new MenuListener();
		jpm = new JPopupMenu();
		jmiRemove = new JMenuItem("Remove");
		jpm.add(jmiRemove);
		
		
		if (courses.length == 1) {
			jlCourse = new JLabel(courses[0].getName() + " (" + courses[0].getCredits() + ")");
			this.jlCourse.addMouseListener(CML);
			this.jlCourse.setCursor(new Cursor(Cursor.HAND_CURSOR));
			this.jlCourse.setSize(200, 30);
			this.add(jlCourse);
		}
		else {
			this.jcbCourses = new JComboBox();
			this.jcbCourses.addItem("Choose One");
			for (int i = 0; i<courses.length; i++) {
				this.jcbCourses.addItem(courses[i].getName() + " (" + courses[i].getCredits() + ")");
			}
			
			this.jcbCourses.addItemListener(CL);
			this.jcbCourses.addMouseListener(CML);
			this.jcbCourses.setCursor(new Cursor(Cursor.HAND_CURSOR));
			this.add(jcbCourses);
		}
		
		
		//this.add(jbOptions);
	}
	
	public SwapClassDisplay() {
		this.setPreferredSize(new Dimension(125, 20));
		this.setBackground(MainWindow.MAINCOLOR);
		this.setBorder(new LineBorder(Color.black));
		this.PLACEHOLDER = true;
	}
	
	private class ChangeListener implements ItemListener {

		@Override
		public void itemStateChanged(ItemEvent ie) {
			// TODO Auto-generated method stub
			int index = jcbCourses.getSelectedIndex();
			if (index != 0) {
				classInfo.update(courses[index-1]);
			}
		}
		
	}
	
	private class ClassMouseListener implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent me) {
			// TODO Auto-generated method stub
			if (me.getButton() == MouseEvent.BUTTON1) {
				if(courses.length == 1) {
					classInfo.update(courses[0]);
				}
				else {
					if (jcbCourses.getSelectedIndex() != 0) classInfo.update(courses[jcbCourses.getSelectedIndex()-1]);
				}
			}
			else if (me.getButton() == MouseEvent.BUTTON3) {
				jpm.show(jbOptions, 0, 0);
			}
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	private class MenuListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			
			if (ae.getSource() == jmiRemove) {
				
			}
		}
		
	}
	private class OptionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			jpm.show(jbOptions, 0, 0);
		}
		
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}

	public void setCourseGroup(ScheduledCourseGroup courseGroup) {
		this.courseGroup = courseGroup;
	}

	public ScheduledCourseGroup getCourseGroup() {
		return courseGroup;
	}

}

